﻿using System;
using System.IO;
using System.Windows.Forms;
using PPI.Client.Interface;
using PPI.Client.Presenter;
using PPI.Client.Utils;

namespace PPI
{
    public partial class frmDeployBase : frmBase, IDeployDatabase
    {
        #region - Attributes -
        
        private bool m_isDownloadingDatabase;
        protected DeployDatabasePresenterBase m_Presenter;

        #endregion

        #region - Constructors -

        public frmDeployBase()
        {
            InitializeComponent();
            MenuMain.Visible = false;
            FormSize = FormSizes.Medium;
        }

        #endregion

        #region - IDeployDatabase - 

        /// <summary>
        /// Sets the progress bar value
        /// </summary>
        public int ProgressBarDownload
        {
            set { pbDownloadFile.Value = value; }
        }

        /// <summary>
        /// Sets the progress bar download text
        /// </summary>
        public string ProgressDownloadMessage
        {
            set { lblDownloadingDatabase.Text = value; }
        }

        public void ShowAnotherInspectionMessage()
        {
            DoNewDeploy();
        }

        public bool IsDownloading
        {
            set { m_isDownloadingDatabase = value; }
        }

        #endregion

        #region - Event handlers -

        protected override void Form_Load(object sender, EventArgs e)
        {
            base.Form_Load(sender, e);
            btnDeploy.Enabled = false;

            //try
            //{
            //    m_Presenter = new DeployDatabasePresenter(this);
            //}
            //catch (Exception ex)
            //{
            //    RegisterError.SaveError(ex);
            //}
        }

        private void btnDeploy_Click(object sender, EventArgs e)
        {
            DoDeploy();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmDeployBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_isDownloadingDatabase)
                e.Cancel = true;
        }

        #endregion

        #region - Public Methods -

        /// <summary>
        /// Activates the deploy
        /// </summary>
        public void ActivateDeploy()
        {
            btnDeploy.Enabled = ValidateAll();
        }

        /// <summary>
        /// Called after the task has finished
        /// </summary>
        protected virtual void DoNewDeploy()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Executes the deploy actions
        /// </summary>
        protected virtual void DoDeploy()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Opens a brownserdialog and selects a valid mobile database File returning a FileInfo
        /// </summary>
        /// <returns>Selected destiny file info</returns>
        protected static DirectoryInfo SelectDestinationFolder()
        {
            FolderBrowserDialog _mobileDatabaseBrownser = new FolderBrowserDialog();
            _mobileDatabaseBrownser.ShowNewFolderButton = true;
            
            DialogResult _dialogResult = _mobileDatabaseBrownser.ShowDialog();
            DirectoryInfo _selectedDestinationDirectory = null;

            if (_dialogResult == DialogResult.OK)
                _selectedDestinationDirectory = new DirectoryInfo(_mobileDatabaseBrownser.SelectedPath);
            
            return _selectedDestinationDirectory;
        }

        #endregion
    }
}
